from ..database import database, TABLE_PREFIX

from peewee import *


class Setting(database.Model):
    id = PrimaryKeyField(db_column='id')
    code = CharField(db_column='code', max_length=80, help_text='配置代码')
    screen_name = CharField(db_column='screen_name', max_length=200, help_text='显示名称')
    value = CharField(db_column='value', max_length=400, help_text='配置值')
    value_mode = CharField(db_column='value_mode', max_length=15, help_text='配置值类型', default=True)
    type = CharField(db_column='type', max_length=10, help_text='配置类型(system的不可删除)', default='system')
    status = CharField(db_column='status', max_length=15, default='normal')

    # class Meta:
    #     db_table = '{}_{}'.format(TABLE_PREFIX, 'settings')

    @classmethod
    def get_setting(cls, code):
        setting = Setting.get_or_none(Setting.code== code)
        if not setting: return None
        return setting.value

    @classmethod
    def get_recommend_fee_rate(cls):
        rate = cls.get_or_none(cls.code == 'RECOMMEND_FEE_RATE')
        if rate:
            return int(rate.value) / 100
        return 0
